package org.hong.monkey.util

import org.hong.monkey.{MonkeyEnv, MonkeyConf}
import org.hong.monkey.rpc.{RpcAddress, RpcEndpointRef, RpcEnv, RpcTimeout}

/**
 */
object RpcUtils {

  def makeDriverRef(name:String, conf:MonkeyConf, rpcEnv:RpcEnv): RpcEndpointRef = {
    val driverActorSystemName = MonkeyEnv.driverActorSystemName
    val driverHost: String = conf.get("monkey.driver.host", "localhost")
    val driverPort: Int = conf.getInt("monkey.driver.port", 7077)
    Utils.checkHost(driverHost, "Expected hostname")
    rpcEnv.setupEndpointRef(driverActorSystemName, RpcAddress(driverHost, driverPort), name)
  }

  def numRetries(conf: MonkeyConf): Int = {
    conf.getInt("monkey.rpc.numRetries", 3)
  }

  def retryWaitMs(conf: MonkeyConf): Long = {
    conf.getTimeAsMs("monkey.rpc.retry.wait", "3s")
  }

  private[monkey] def askRpcTimeout(conf: MonkeyConf): RpcTimeout = {
    RpcTimeout(conf, Seq("monkey.rpc.askTimeout", "monkey.network.timeout"), "120s")
  }

  private[monkey] def lookupRpcTimeout(conf: MonkeyConf): RpcTimeout = {
    RpcTimeout(conf, Seq("monkey.rpc.lookupTimeout", "monkey.network.timeout"), "120s")
  }
}
